package com.project.gcsj5.controller;

import com.project.gcsj5.common.Result;
import com.project.gcsj5.model.Recommend;
import com.project.gcsj5.service.RecommendService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
public class RecommendController {
    @Autowired private RecommendService service;


    @GetMapping("/recommend/{id}")
    public Result GetByid(@PathVariable int id){
        Recommend recommend = new Recommend();
        recommend = service.recommends_by_id(id);
        if(recommend != null)
        {return Result.success(recommend);}
        else
        {return Result.fail();}
    }

    @GetMapping("/recommend/user/{id}")
    public Result Get_User_recommends(@PathVariable int id){
        List<Recommend> s = new ArrayList<>();
        s = service.recommends_user(id);
        if(s!=null)
        {return  Result.success(s);}
        else
        {return Result.fail();}
    }

    @GetMapping("/recommend")
    public Result Getlist(){
        List<Recommend> s = new ArrayList<>();
        int len = 0;
        len=service.list().size();
        if(s!=null)
        {return  Result.success(len);}
        else
        {return Result.fail();}
    }

    @GetMapping("/recommends/{pages}")
    public Result Getlist(@PathVariable int pages){
        List<Recommend> s = new ArrayList<>();
        s=service.recommends_list(pages);
        if(s!=null)
        {return  Result.success(s);}
        else
        {return Result.fail();}
    }

    @GetMapping("/recommend/s01/{content}")
    public Result search_by_content(@PathVariable("content") String content){
        List<Recommend> recommends = new ArrayList<>();
        recommends = service.recommends_content(content,1);
        if(recommends!=null)
        {return Result.success(recommends);}
        else
        {return Result.fail();}
    }

    @GetMapping("/recommend_add/{id}")
    public Result add_view(@PathVariable int id){
        Boolean recommend = false;
        recommend = service.add_view(id);
        if(recommend != null)
        {return Result.success(recommend);}
        else
        {return Result.fail();}
    }

    @DeleteMapping("/recommend/{id}")
    public Result remove(@PathVariable int id){
        Boolean a = service.removeById(id);
        if(a)
        {return Result.success(a);}
        else
        {return Result.fail();}
    }

    @GetMapping("/hot/recommend")
    public Result Get_hot_list(){
        List<Recommend> s = new ArrayList<>();
        s=service.recommends_hot();
        if(s!=null)
        {return  Result.success(s);}
        else
        {return Result.fail();}
    }

}
